﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Guline.Web.XjsGame.Controllers
{
    public class ContentController : BaseController
    {

        public ActionResult Create()
        {

            return View();
        }
        // GET: /Content/
        public ActionResult Type(int ID, int? newsPage)
        {
            var cat = iCats.Where(m => m.ID == ID).FirstOrDefault();

            if (cat == null)
                return HttpNotFound();
            else
            {
                if (!newsPage.HasValue || newsPage < 1)
                {
                    newsPage = 1;
                }
                ViewBag.Cat = cat;
                string scatid=ID.ToString();
                var news = db.contents.Where(m => m.status == 0 && m.danhmuc == scatid).OrderByDescending(m => m.id);
                int pagesize = 10;
                double total = news.Count();
                if (total == 0)
                {
                    ViewBag.newsPage = 1;
                    return View(news);
                }
                int pages = (int)Math.Ceiling(total / pagesize);
                if (newsPage > pages)
                {
                    newsPage = pages;
                }
                ViewBag.newsPage = newsPage + 1;
                return View(news.Skip(((int)newsPage - 1) * pagesize).Take(pagesize));
            }
        }
        public ActionResult getNewsTypePage(int newsPage,string CatID)
        {
            if (newsPage < 1)
            {
                newsPage = 1;
            }
            var news = db.contents.Where(m => m.status == 0 && m.danhmuc == CatID).OrderByDescending(m => m.id);
            int pagesize = 10;
            double total = news.Count();
            int pages = (int)Math.Ceiling(total / pagesize);
            if (newsPage > pages)
            {
                newsPage = pages;
                var mynews = news.Skip((newsPage - 1) * pagesize).Take(pagesize);
                var myencode = from n in mynews select new { n.id, n.name, n.date_added, n.image, n.detail_short };
                return Json(new { ismax = true, max = pages }, JsonRequestBehavior.AllowGet);
            }
            else
            {
                var mynews = news.Skip((newsPage - 1) * pagesize).Take(pagesize);
                var myencode = from n in mynews select new { n.id, n.name, n.date_added, n.image, n.detail_short };
                return Json(new { ismax = false, list = myencode }, JsonRequestBehavior.AllowGet);
            }
        }
        //getNewsTypePage
        public ActionResult CGoTo(int ID)
        {
            var cat = iCats.Where(m => m.ID == ID).FirstOrDefault();
      
            if (cat==null)
                return HttpNotFound();
            else
                return RedirectToAction("Type", new { ID = ID, Title = Guline.Web.XjsGame.Infrastructure.Utils.UrlRewriteString(cat.Name) });
        }
        public ActionResult IGoTo(int ID)
        {
            var content = db.contents.Where(m => m.id == ID && m.danhmuc != "0").FirstOrDefault();
            if (content == null)
                return HttpNotFound();
            else
                return RedirectToAction("Detail", new {ID=ID,Title=Guline.Web.XjsGame.Infrastructure.Utils.UrlRewriteString(content.name)});
        }
        public ActionResult Detail(int ID,string Title)
        {
            var content = db.contents.Where(m => m.id == ID).FirstOrDefault();
            if (content == null)
                return HttpNotFound();
            else
            {
                var tinlienquan = db.contents.Where(m => m.danhmuc == content.danhmuc && m.id != content.id&&m.status==0).OrderByDescending(m => m.id).Take(10).ToList();
                ViewBag.TinLienQuans = tinlienquan;
                content.luotxem += 1;
                db.SaveChanges();
                return View(content);
            }
        }
    }
}
